﻿using System;
using System.Windows.Forms;

namespace TCRestoDruid.UI
{
    public partial class TCRestoDruidSettingsForm : Form
    {
        public TCRestoDruidSettingsForm()
        {
            InitializeComponent();
        }

        private void TCRestoDruidSettingsForm_Load(object sender, EventArgs e)
        {
            foreach (var p in TCRestoDruidSettings.Instance.GetSettings())
            {
                foreach (GroupBox gb in settingsPanel.Controls)
                {
                    foreach (object obj in gb.Controls)
                    {
                        if (obj is CheckBox)
                        {
                            CheckBox checkBox = (CheckBox) obj;

                            if (checkBox.Name == p.Key)
                            {
                                checkBox.Checked = (bool) p.Value;
                                break;
                            }
                        }

                        else if (obj is ProgressBar) 
                        { 
                            ProgressBar progressBar = (ProgressBar) obj;

                            if (progressBar.Name == p.Key) 
                            {
                                progressBar.Value = Int32.Parse(p.Value.ToString()); 
                                break; 
                            } 
                        }
                        
                        else if (obj is NumericUpDown)
                        {
                            NumericUpDown numericUpDown = (NumericUpDown)obj;

                            if (numericUpDown.Name == p.Key) 
                            {
                                numericUpDown.Value = Int32.Parse(p.Value.ToString()); 
                                break; 
                            }
                        }
                    }
                }
            }
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            TCRestoDruidSettings.Instance.Save();
            Close();
        }

        private void buttonRestore_Click(object sender, EventArgs e)
        {
            TCRestoDruidSettings.Instance.InitializeDefaultValues();
            OnLoad(e);
        }

        private void Log_CheckedChanged(object sender, EventArgs e)
        {
            TCRestoDruidSettings.Instance.Log = ((CheckBox)sender).Checked;
        }

        private void UseTree_CheckedChanged(object sender, EventArgs e)
        {
            TCRestoDruidSettings.Instance.UseTree = ((CheckBox)sender).Checked;
        }

        private void UseBarkskin_CheckedChanged(object sender, EventArgs e)
        {
            TCRestoDruidSettings.Instance.UseBarkskin = ((CheckBox)sender).Checked;
        }

        private void UseNaturesGrasp_CheckedChanged(object sender, EventArgs e)
        {
            TCRestoDruidSettings.Instance.UseNaturesGrasp = ((CheckBox)sender).Checked;
        }

        private void UseRebirth_CheckedChanged(object sender, EventArgs e)
        {
            TCRestoDruidSettings.Instance.UseRebirth = ((CheckBox)sender).Checked;
        }

        private void UseRevive_CheckedChanged(object sender, EventArgs e)
        {
            TCRestoDruidSettings.Instance.UseRevive = ((CheckBox)sender).Checked;
        }

        private void UseRemoveCurse_CheckedChanged(object sender, EventArgs e)
        {
            TCRestoDruidSettings.Instance.UseRemoveCurse = ((CheckBox)sender).Checked;
        }

        private void OnlyHealInstances_CheckedChanged(object sender, EventArgs e)
        {
            TCRestoDruidSettings.Instance.OnlyHealInstances = ((CheckBox)sender).Checked;
        }

        private void HealthPercent_MouseAction(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }

            ProgressBar bar = (ProgressBar) sender;
            TCRestoDruidSettings.Instance.HealthPercent = bar.Value = MousePositionValue(e.Location.X, bar.Width);
        }

        private void ManaPercent_MouseAction(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }

            ProgressBar bar = (ProgressBar)sender;
            TCRestoDruidSettings.Instance.ManaPercent = bar.Value = MousePositionValue(e.Location.X, bar.Width);
        }

        private void SwiftmentPercent_MouseAction(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }

            ProgressBar bar = (ProgressBar)sender;
            TCRestoDruidSettings.Instance.SwiftmendPercent = bar.Value = MousePositionValue(e.Location.X, bar.Width);
        }

        private void RejuvenationPercent_MouseAction(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }

            ProgressBar bar = (ProgressBar)sender;
            TCRestoDruidSettings.Instance.RejuvenationPercent = bar.Value = MousePositionValue(e.Location.X, bar.Width);
        }

        private void NourishPercent_MouseAction(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }

            ProgressBar bar = (ProgressBar)sender;
            TCRestoDruidSettings.Instance.NourishPercent = bar.Value = MousePositionValue(e.Location.X, bar.Width);
        }

        private void RegrowthPercent_MouseAction(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }

            ProgressBar bar = (ProgressBar)sender;
            TCRestoDruidSettings.Instance.RegrowthPercent = bar.Value = MousePositionValue(e.Location.X, bar.Width);
        }

        private void SOSHealingPercent_MouseAction(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }

            ProgressBar bar = (ProgressBar)sender;
            TCRestoDruidSettings.Instance.SOSHealingPercent = bar.Value = MousePositionValue(e.Location.X, bar.Width);
        }

        private void WildGrowthPercent_MouseAction(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }

            ProgressBar bar = (ProgressBar)sender;
            TCRestoDruidSettings.Instance.WildGrowthPercent = bar.Value = MousePositionValue(e.Location.X, bar.Width);
        }

        private void TranquilityPercent_MouseAction(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }

            ProgressBar bar = (ProgressBar)sender;
            TCRestoDruidSettings.Instance.TranquilityPercent = bar.Value = MousePositionValue(e.Location.X, bar.Width);
        }

        private void TreePercent_MouseAction(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }

            ProgressBar bar = (ProgressBar)sender;
            TCRestoDruidSettings.Instance.TreePercent = bar.Value = MousePositionValue(e.Location.X, bar.Width);
        }

        private void PercentOfPlayersAliveInNeedOfHealing_MouseAction(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }

            ProgressBar bar = (ProgressBar)sender;
            TCRestoDruidSettings.Instance.PercentOfPlayersAliveInNeedOfHealing = bar.Value = MousePositionValue(e.Location.X, bar.Width);
        }

        private void LifebloomCount_ValueChanged(object sender, EventArgs e)
        {
            TCRestoDruidSettings.Instance.LifebloomCount = (int) ((NumericUpDown)sender).Value;
        }

        //CREDITS: Fpsware
        public int MousePositionValue(double mousePosition, double progressBarWidth)
        {
            if (mousePosition < 0) mousePosition = 0;
            if (mousePosition > progressBarWidth) mousePosition = progressBarWidth;

            double ratio = mousePosition / progressBarWidth;
            double value = ratio * 100;

            if (value > 100) value = 100;
            if (value < 0) value = 0;

            return (int)Math.Ceiling(value);
        }
    }
}
